/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.made2ordersoftware.drinkmaker.business;

import com.made2ordersoftware.drinkmaker.dao.DrinkDao;
import com.made2ordersoftware.drinkmaker.dao.DrinkIngredientsDao;
import com.made2ordersoftware.drinkmaker.dao.IngredientDao;
import com.made2ordersoftware.drinkmaker.domain.DrinkEntity;
import com.made2ordersoftware.drinkmaker.domain.DrinkIngredientEntity;
import com.made2ordersoftware.drinkmaker.domain.IngredientEntity;
import com.made2ordersoftware.drinkmaker.domain.enums.GlassType;
import com.made2ordersoftware.exceptions.DAOException;
import com.made2ordersoftware.util.StringUtility;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityExistsException;
import javax.persistence.NoResultException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Stateless
public class DrinkMgr {

    private static final Logger LOG = LoggerFactory.getLogger(DrinkMgr.class);
    @EJB
    private DrinkDao drinkDao;
    @EJB
    private DrinkIngredientsDao drinkIngredientsDao;
    @EJB
    private IngredientDao ingredientDao;

    /**
     *
     * @param drinkName
     * @param instructions
     * @param glass
     * @param image
     * @param ingredient1
     * @param ingredient2
     * @param ingredient3
     * @param ingredient4
     * @param ingredient5
     * @param amount1
     * @param amount2
     * @param amount3
     * @param amount4
     * @param amount5
     *
     * @throws EntityExistsException
     * @throws DAOException
     */
    public void addDrink(String drinkName, String instructions, GlassType glass, String image,
            String ingredient1, String ingredient2, String ingredient3, String ingredient4,
            String ingredient5, String amount1, String amount2, String amount3, String amount4,
            String amount5) throws EntityExistsException, DAOException {

        drinkDao.findByName(drinkName);

        List<DrinkIngredientEntity> ingredients = new ArrayList<>();
        if (!StringUtility.ifNullOrEmpty(ingredient1)) {
            createDrinkIngredient(ingredient1, amount1, ingredients);
        }
        if (!StringUtility.ifNullOrEmpty(ingredient2)) {
            createDrinkIngredient(ingredient2, amount2, ingredients);
        }
        if (!StringUtility.ifNullOrEmpty(ingredient3)) {
            createDrinkIngredient(ingredient3, amount3, ingredients);
        }
        if (!StringUtility.ifNullOrEmpty(ingredient4)) {
            createDrinkIngredient(ingredient4, amount4, ingredients);
        }

        DrinkEntity drink = new DrinkEntity(drinkName, instructions, ingredients);
        drinkDao.create(drink);

    }

    /**
     *
     * @param drinkName
     */
    public void searchByDrink(String drinkName) {
    }

    /**
     *
     * @param ingredient
     */
    public void searchByIngredient(String... ingredient) {
    }

    /**
     *
     * @param ingredientName
     * @param amount
     * @param ingredients
     *
     * @throws DAOException
     * @throws EntityExistsException
     */
    private void createDrinkIngredient(String ingredientName, String amount,
            List<DrinkIngredientEntity> ingredients) throws DAOException, EntityExistsException {
        IngredientEntity ingredient = verifyIngredient(ingredientName);
        DrinkIngredientEntity drinkIngredient = new DrinkIngredientEntity(ingredient, amount);
        drinkIngredientsDao.create(drinkIngredient);
        ingredients.add(drinkIngredient);
    }

    /**
     *
     * @param ingredientName
     *
     * @return
     *
     * @throws EntityExistsException
     * @throws DAOException
     */
    private IngredientEntity verifyIngredient(String ingredientName) throws EntityExistsException,
            DAOException {

        IngredientEntity ingredient;
        try {
            ingredientDao.findByName(ingredientName);
            ingredient = new IngredientEntity(ingredientName);
        } catch (NoResultException e) {
            ingredient = new IngredientEntity(ingredientName);
            ingredientDao.create(ingredient);
        }
        return ingredient;
    }

}
